< previous page page_348 next page >

Page 348
9f31265bb40b592b1929e18a47ab28e5.gif
Public Sub Error3()

   ' This line will cause an error
   ' with a zero divisor
   txtResult = CInt(txtDividend) / CInt(txtDivisor)

   MsgBox "Calculation Complete"
End Sub
This is why its very important to put error handling into every procedure that could conceivably generate an error. If you dont do this, youll never be sure where an error comes from, and this will be a very hard bug to track down.
Turning Error Handling Off
Error-handling routines are disabled automatically as soon as the procedure in which they are executed returns to the calling procedure. To turn error handling off within the same procedure, you can put the following line in your program:
9f31265bb40b592b1929e18a47ab28e5.gif
On Error Goto 0
Creating a Global Error Handler
Obviously, you could end up writing a lot of duplicate code if you attempt to handle the same error in every procedure. So you might want to think about creating a generic error-handling routine. In this routine, you could create a big Select Case statement to check for specific errors. It could either attempt to fix them or just give up. You can also choose to display a better dialog box than the normal Visual Basic error dialog box. Figure 17.6 shows an example of a dialog box that gives you a little more information than the normal Visual Basic error dialog box.
8160-0348a.gif
Figure 17.6
A generic error form.

 
< previous page page_348 next page >

If you like this book, buy it!